﻿using Client.IConfiguration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Client
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override  void OnStartup(StartupEventArgs e)
        {
            //IConfigurationHelp IConfigurationHelp_ = HelpTools.IOC.Getobject<IConfiguration.IConfigurationHelp>();
            //string? uir = IConfigurationHelp_.ReadNode("ServerupdateUir");
            //if (uir != null)
            //{
            //    using (HttpClient client = new HttpClient())
            //    {
            //        try
            //        {
            //            HttpResponseMessage response = client.GetAsync(uir).Result;
            //            string data = response.Content.ReadAsStringAsync().Result;
            //            string? version_ = IConfigurationHelp_.ReadNode("version");
            //            if (version_ != null && data != null)
            //            {
            //                if (Convert.ToInt16(data.Replace(".", "")) > Convert.ToInt16(version_.Replace(".", "")))
            //                {
            //                    string currentPath = System.AppDomain.CurrentDomain.BaseDirectory;
            //                    Process.Start($"{currentPath}\\Update\\Update.exe");
            //                    App.Current.Shutdown();
            //                }
            //            }
            //        }
            //        catch (Exception ex)
            //        {
                        
            //        }
            //    }
            //}
            //在异常由应用程序引发但未进行处理时发生。UI线程
            //无法捕获多线程异常
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            //专门捕获所有线程中的异常
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            //专门捕获Task异常
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
        }

        private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
        {
            var Dialog_ = HelpTools.IOC.Getobject<MessageDialog.Dialog>();
            Dialog_.OpenDialog = false;
            WPFDevelopers.Minimal.Controls.MessageBox.Show(e.Exception.Message, "消息", MessageBoxButton.OK, MessageBoxImage.Error);
            e.SetObserved();
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var Dialog_ = HelpTools.IOC.Getobject<MessageDialog.Dialog>();
            Dialog_.OpenDialog = false;
            WPFDevelopers.Minimal.Controls.MessageBox.Show((e.ExceptionObject as Exception).Message, "消息", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            var Dialog_ = HelpTools.IOC.Getobject<MessageDialog.Dialog>();
            Dialog_.OpenDialog = false;
            //Dialog_.OpenMessage("Task线程异常:" + e.Exception.Message);
            WPFDevelopers.Minimal.Controls.MessageBox.Show(e.Exception.Message, "消息", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}
